"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Room = exports.RoomState = void 0;
const Config_1 = require("../Config");
const GameMgr_1 = require("../game/GameMgr");
const Watcher_1 = require("../game/other/Watcher");
const MySQLService_1 = __importDefault(require("../service/MySQLService"));
const Logger_1 = __importDefault(require("../utils/Logger"));
const HallMgr_1 = __importDefault(require("./HallMgr"));
var RoomState;
(function (RoomState) {
    RoomState[RoomState["MATCHING"] = 0] = "MATCHING";
    RoomState[RoomState["GAMEING"] = 1] = "GAMEING";
    RoomState[RoomState["CLOSE"] = 2] = "CLOSE"; //已关闭
})(RoomState = exports.RoomState || (exports.RoomState = {}));
class Room {
    constructor(id, cipher = null) {
        this.id = -1;
        this.cipher = null; //房间暗号 四个数字，string类型
        this.users = new Array(); //玩家
        this.state = RoomState.MATCHING; //房间状态
        this.id = id;
        this.cipher = cipher;
        Logger_1.default.log("房间创建：id = " + id + ",cipher = " + cipher);
    }
    /** 是否可以加入房间 */
    checkAddPlayer() {
        return this.state == RoomState.MATCHING;
    }
    /** 检查密码房能否加入玩家 */
    checkCipherRoomAddPlayer(cipher) {
        return cipher == this.cipher && this.state == RoomState.MATCHING;
    }
    /** 添加玩家 */
    addPlayer(user) {
        this.users.push(user);
        user.room = this;
        if (Config_1.Config.DEBUG || this.users.length == 3) {
            this.startGame();
        }
    }
    /** 添加观众 */
    addWatcher(watcher) {
        Logger_1.default.log("添加观众");
        new Watcher_1.Watcher(this.gameMgr.manual, watcher);
    }
    /** 开始游戏 */
    startGame() {
        Logger_1.default.log("房间号:", this.id, "开始游戏");
        this.state = RoomState.GAMEING;
        HallMgr_1.default.updateOnline();
        this.gameMgr = new GameMgr_1.GameMgr(this, this.users);
        for (let user of this.users) {
            MySQLService_1.default.updateUserRoomId(user.username, this.id);
        }
    }
    /** 房间解散 */
    closeRoom() {
        this.state = RoomState.CLOSE;
        this.cipher == null ? HallMgr_1.default.rooms.delete(this) : HallMgr_1.default.cipherRooms.delete(this);
        this.gc();
        Logger_1.default.log("房间解散");
    }
    /** 内存清空 */
    gc() {
        var _a;
        (_a = this.gameMgr) === null || _a === void 0 ? void 0 : _a.gc();
        this.gameMgr = null;
        this.users.forEach(user => {
            user.room = null;
            user.player = null;
            user.watcher = null;
            MySQLService_1.default.updateUserRoomId(user.username, 0);
        });
        this.users = null;
    }
    /** 取消匹配 */
    cancelMatch(user) {
        if (this.state == RoomState.MATCHING) {
            for (let i = 0; i < this.users.length; i++) {
                if (this.users[i] == user) {
                    this.users.splice(i, 1);
                }
            }
            if (this.users.length == 0) {
                this.cipher == null ? HallMgr_1.default.rooms.delete(this) : HallMgr_1.default.cipherRooms.delete(this);
                this.closeRoom();
            }
            user.room = null;
            Logger_1.default.log("取消匹配");
        }
        else {
            Logger_1.default.log("取消匹配失败");
        }
    }
    /** 玩家掉线，检查一下是不是全掉线了 */
    checkPlayersOnLineState(user) {
        var _a, _b;
        if (Config_1.Config.DEBUG) {
            this.closeRoom();
            return;
        }
        // 还没写掉线重连逻辑，掉线相当于死亡
        (_b = (_a = user.player) === null || _a === void 0 ? void 0 : _a.small) === null || _b === void 0 ? void 0 : _b.die(user.player);
        // let online = 0
        // for (let player of this.users) {
        //     if(player.is_onLine) {
        //         online++
        //     }
        // }
        // if(online == 0) {
        //     this.closeRoom()
        // }
    }
    /** 认输 */
    admitDefeat(user) {
        user.player.small.die(user.player);
    }
}
exports.Room = Room;
